package com.server.rule.component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;

//import org.jdom.Element;

import com.server.rule.component.integrated.RuleDecisionFactory;
import com.server.rule.data.ExceptionTypeData;

import com.server.rule.data.Data;
import com.shared.rule.TypeDecision;

public class BaseComponent extends AbstractRuleComposite{
	
	public BaseComponent() {
		nextRules = new ArrayList<RuleComponent>();
	}
	
	@Override
	public void operate(Data data,DecisionHandler handler) throws ExceptionTypeData {			
		for(RuleComponent r : nextRules){
			r.operate(data,handler);
		}		
	}
	
	
	@Override
	public void generateFromXML(Element xmlElement) {
		Iterator iteratorElement = (xmlElement.getChildren("Rule")).iterator();
		while(iteratorElement.hasNext()){
			RuleComponent ruleTemp = null;
			Element elementTemp = (Element) iteratorElement.next();
			if(elementTemp.getAttributeValue("type").equals("Composite")){
				ruleTemp=new RuleComposite();
			}else{
				String type = elementTemp.getAttributeValue("type");
				ruleTemp = RuleDecisionFactory.get().createDecision(TypeDecision.getTypeDecision(type));
			}
			ruleTemp.generateFromXML(elementTemp);
			add(ruleTemp);			
		}
		
	}

	@Override
	public Element getXMLElement() {
		Element xmlElement = new Element("BaseComponent");
		xmlElement.setAttribute("type", "BaseComponent");		
		for(RuleComponent r : nextRules){
			xmlElement.addContent(r.getXMLElement());
		}
		return xmlElement;
	}

	@Override
	public void getUsedVar(List<String> listeUsedVar) {
		for(RuleComponent r : nextRules) r.getUsedVar(listeUsedVar);		
	}
	
}
